exports.search_organization_order = function(req,res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var skip    = restfulParams.skip  == undefined ? 0 : restfulParams.skip;
    var limit   = restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;
    var sort = restfulParams.sort; //1 智能 销量  2 人气
    // if(sort == 1){
    //     var condition = "/sort_order/o_course_sales-desc";
    // }else if(sort == 2){
    //     var condition = "/sort_order/o_browse_times-desc";
    // }
    //任务流创建
    async.waterfall([

            //1.查询附近机构
            function(cb){
                console.log(12);
                R.SEND_HTTP(
                    req,
                    {
                        url    : CORE_APIConfig.coreAPI.search_organization_sort+ "/lng/"+restfulParams.lng+"/lat/"+restfulParams.lat+"/skip/" + skip + "/limit/" + limit+"/sort/"+sort,
                        method : 'POST',
                        data : {
                            fields : [
                                "o_id",
                                "o_name",
                                "o_logo",
                                "o_tags",
                                "o_address",
                                "is_cashback",
                                "o_course_sales",
                                "o_browse_times",
                                // "is_cashback"
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        console.log(data.data);
                        console.log(10);
                        resultData.organizationData = data.data;

                        cb();
                    }
                );
            },

            //查询每个结构是否有约课课程
            function(cb){

                //创建添加任务
                var tasks = [];

                //循环节点列表
                resultData.organizationData.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        //查询机构最新的一条约课课程信息

                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.search_organization_course + "/oc_oid/ " + item.o_id + "/oc_type/1/skip/0/limit/1",
                                method: 'POST',
                                data: {
                                    fields: ["oc_id","oc_title","oc_course_count","oc_new_price"].join(",")
                                }
                            },
                            function (err, data) {
                                //判断是否有错误信息
                                if (err) {
                                    return cb(err, data);
                                }

                                item.courseData = data.data;

                                cb();
                            }
                        );

                    });

                });

                async.waterfall(tasks, cb);

            }


        ],function(err, result){

            //返回最终拼接结果
            RES.response(res,true,resultData);

        }
    );

}